/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.device.server.processor;

import org.apache.logging.log4j.*;

import com.bugull.farm.core.cache.PropertiesCache;
import com.bugull.farm.core.frame.structure.Frame;
import com.bugull.farm.core.handler.Processor;
import com.bugull.farm.core.utils.BufUtil;
import com.bugull.farm.core.utils.ByteUtil;
import com.bugull.redis.RedisConnection;
import com.bugull.redis.mq.MQClient;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

/**
 * command from device. Just publish message to app topic.
 * 
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class ControlProcessor implements Processor {

	private final Logger logger = LogManager.getLogger(ControlProcessor.class);
    @Override
    public void process(ChannelHandlerContext ctx, Frame frame) {
    	try{
    		byte[] msg = frame.getRequest();
	        MQClient client = RedisConnection.getInstance().getMQClient();
	        client.publish(PropertiesCache.getInstance().get("redis.topic.app"), msg);
	        logger.debug("send to "+PropertiesCache.getInstance().get("redis.topic.app") + frame.getHexRequest());
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("ControlProcessor can't deal the message "+frame.getHexRequest(),e);
    	}
    }

}
